package models

import (
	"fmt"
	"gorm.io/gorm"
)

type Role struct {
	gorm.Model
	Code        int
	RoleName    string
	RolePid     int
	RoleComment string
	Users       []*User   `gorm:"many2many:user_roles;"`
	Routers     []*Router `gorm:"many2many:router_roles;"`
}

//func (this *Role) TableName() string {
//	return "roles"
//}
func (this *Role) String() string {
	return fmt.Sprintf("ID:%d 角色名:%s", this.ID, this.RoleName)
}

//func NewRole(id, pid int, roleName, roleComment string) (*Role, error) {
//	role := &Role{}
//	role.ID = uint(id)
//	role.RolePid = pid
//	role.RoleName = roleName
//	role.RoleComment = roleComment
//
//	return role, nil
//}
func NewRole(code, pid int, roleName, roleComment string, routers []*Router) (*Role, error) {
	role := &Role{}
	role.Code = code
	role.RolePid = pid
	role.RoleName = roleName
	role.RoleComment = roleComment
	role.Routers = routers

	return role, nil
}
